"""

 * Skype.py
 * This file is part of skype-plugin
 *
 * Copyright (C) 2007 - Jakub Krajniak
 *
 * skype-plugin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * any later version.
 *
 * skype-plugin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with skype-plugin; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 
"""

import sys
import traceback
import gobject
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

class Callback_obj(dbus.service.Object):
	def __init__(self, bus, object_path, out):
		self.out = out
		try:
			dbus.service.Object.__init__(self, bus, object_path, bus_name='com.Skype.API')
		except:
			pass

	@dbus.service.method(dbus_interface='com.Skype.API')
	def Notify(self, message_text):
		args = message_text.split(None)
		if args[0] == 'CALL':
			call_id = args[1]
			if args[2] == 'STATUS':
				if args[3] == 'RINGING':
					msg = 'ALTER CALL '+call_id+' ANSWER'
					self.Send(msg)

	def Send(self, message_text):
		resp = self.out.Invoke(message_text)
		return resp

class Skype:
	def __init__(self):
		DBusGMainLoop(set_as_default=True)
		self.remote_bus = dbus.SessionBus()
   		self.out_connection = self.remote_bus.get_object('com.Skype.API', '/com/Skype')
   		self.skype = Callback_obj(self.remote_bus, '/com/Skype/Client', self.out_connection)
   		
   	def getStatus(self):
		self.skype.Send('NAME Rhythmbox')
		self.skype.Send('PROTOCOL 5')
		ret = self.skype.Send('GET PROFILE RICH_MOOD_TEXT')
		return ret.replace('PROFILE RICH_MOOD_TEXT ','')
	def setStatus(self,status):
		self.skype.Send('NAME Rhythmbox')
		self.skype.Send('PROTOCOL 5')
		self.skype.Send('SET PROFILE RICH_MOOD_TEXT '+status)
